package com.uzhie.jt808.service.impl;

import com.uzhie.jt808.JT808Constant;
import com.uzhie.jt808.msg.JT808MsgBean;
import com.uzhie.jt808.msg.entity.JT_0200;
import com.uzhie.jt808.service.BaseService;
import com.uzhie.jt808.service.JT808GPSService;
import com.uzhie.jt808.spring.MsgIDMapping;
import com.uzhie.jt808.spring.MsgService;
import com.uzhie.web.service.listener.MQSendService;
import io.netty.buffer.ByteBufUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * Created by TangFei on 2018/8/9.
 * 位置报警类协议
 */
@MsgService(name = "GPS")
public class JT808GPSServiceImpl extends BaseService implements JT808GPSService {

    @Autowired
    @Qualifier(value = "positionSendService")
    private MQSendService mqSendService;


    /**
     * 终端位置信息
     *
     * @param msgBean
     * @return
     */
    @Override
    @MsgIDMapping(msgID = JT808Constant.MSG_ID.DEV_REQ_POSITION)
    public JT808MsgBean devPosition(JT808MsgBean msgBean) {
        // 响应通用应答
        logger.trace("收到的消息体数据为：" + ByteBufUtil.hexDump(msgBean.getBodyBytes()));
        // 位置基本信息 28 字节
        msgBean.getHeader().getBodyLength();
        JT_0200 instance = new JT_0200();
        instance.ReadFromBytes(msgBean.getBodyBytes());
        mqSendService.sendMessage(JT808Constant.MSG_ID.DEV_REQ_POSITION, msgBean.getHeader().getSimNum(), msgBean.getBodyBytes());
        if (0 != instance.isMediaWarning() && 0 != instance.getAlarmFlag()) {
            // 报警
            // 报警处理确认
            byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 4);
            return super.build808Obj(JT808Constant.MSG_ID.PLA_RES_COMMON, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
        }
        byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 0);
        return super.build808Obj(JT808Constant.MSG_ID.PLA_RES_COMMON, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
    }

    /**
     * 定位数据批量上传 0704
     *
     * @param msgBean
     * @return
     */
    @MsgIDMapping(msgID = JT808Constant.MSG_ID.DEV_REQ_UPLOAD_BATCH_POSITION)
    public JT808MsgBean devPositionBatch(JT808MsgBean msgBean) {
        logger.trace("定位数据批量上传消息体：" + ByteBufUtil.hexDump(msgBean.getBodyBytes()));
        byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 0);
        return super.build808Obj(JT808Constant.MSG_ID.PLA_RES_COMMON, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
    }

}
